@extends('admin.base')

@section('content')
    <div class="layui-card">
        <blockquote class="layui-elem-quote form-inline layui-form" style="font-size: 14px;padding: 10px">
                <div class="layui-inline" style="margin: 3px 0;">
                    <label class="control-label" for="title" style="font-weight: bold">标题</label>：
                    <div class="layui-input-inline">
                        <input type="text" id="title" class="layui-input" name="title" placeholder="请输入标题">
                    </div>
                </div>
                <div class="layui-inline" style="margin: 3px 0;">
                    <label class="control-label" for="status" style="font-weight: bold">状态</label>：
                    <div class="layui-input-inline">
                        <select id="status">
                            <option value="">全部</option>
                            <option value="1">未开始</option>
                            <option value="2">进行中</option>
                            <option value="3">已结束</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline" style="margin: 3px 0;">
                    <label class="control-label" for="start_time1" style="font-weight: bold">开始时间</label>：
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input"  name="start_time1" id="start_time1">
                    </div>
                    <label class="control-label" for="start_time2">-</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="start_time2" id="start_time2">
                    </div>
                </div>
                <div class="layui-inline" style="margin: 3px 0;">
                    <label class="control-label" for="end_time1" style="font-weight: bold">结束时间</label>：
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="end_time1" id="end_time1">
                    </div>
                    <label class="control-label" for="end_time2">-</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="end_time2" id="end_time2">
                    </div>
                </div>

        </blockquote>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="hit_type">
                @{{# if(d.hit_type=='goods_detail'){ }}
                商品详情
                @{{# } if(d.hit_type=='goods_cate'){ }}
                商品分类
                @{{# } if(d.hit_type=='link'){ }}
                连接
                @{{# } if(d.hit_type=='activity'){ }}
                活动
                @{{# } }}
            </script>
            <script type="text/html" id="statusText">
                @{{# if(transformTime(d.start_time) > transformTime(getNow())  ){ }}
                <span class="layui-badge layui-bg-blue">未开始</span>
                @{{# } if( transformTime(d.end_time) <= transformTime(getNow())){ }}
                <span class="layui-badge">已结束</span>
                @{{# } if( transformTime(d.end_time) > transformTime(getNow()) && transformTime(d.start_time)  <= transformTime(getNow()) ){ }}
                <span class="layui-badge layui-bg-green">进行中</span>
                @{{# } }}
            </script>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('operation.operate_position.edit')
                        <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    @endcan
                    @can('operation.operate_position.destroy')
                        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
                    @endcan
                </div>
            </script>
            <script type="text/html" id="thumb">
                <a href="../storage/@{{d.thumb}}" data-fancybox="rfUploadImg">
                    <img src="../storage/@{{d.thumb}}" alt="" width="28" height="28"/>
                </a>
            </script>
            <script type="text/html" id="toolbarDemo">
                @can('operation.operate_position.create')
                <a class="layui-btn layui-btn-sm"  href="{{ route('admin.operate_position.create') }}">添加</a>
                @endcan

                @can('operation.operate_position.destroy')
                <button class="layui-btn layui-btn-sm layui-btn-danger"  lay-event="delete">删除</button>
                @endcan
                <button type="button" class="layui-btn layui-btn-sm" lay-event="search">搜索</button>
            </script>
        </div>
    </div>
@endsection

@section('script')
    @can('operation.operate_position')
        <script>
            layui.use(['layer','table','form'],function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                //用户表格初始化

                var dataTable = table.render({
                    elem: '#dataTable'
                    ,height: 'full-200'
                    ,url: "{{ route('admin.operate_position.data') }}" //数据接口
                    ,page: true //开启分页
                    ,limit: 10 //限制条数
                    ,toolbar: '#toolbarDemo'
                    ,defaultToolbar: ['filter', 'print', 'exports']
                    ,cols: [[ //表头
                        {checkbox: true,fixed: true}
                        ,{field: 'id', title: 'ID',sort: true,width:80}
                        ,{field: 'internal_name', title: '内部名称',width:140}
                        ,{field: 'title', title: '标题',width:140}
                        ,{field: 'abstract', title: '摘要',width:140}
                        ,{field: 'hit_type', title: '命中类型',toolbar: '#hit_type',width:100}
                        ,{field: 'hit_content', title: '命中内容',width:120}
                        ,{field: 'thumb', title: '图片',toolbar: '#thumb',width:80}
                        ,{field: 'weight', title: '权重',sort: true,width:80}
                        ,{field: 'remark', title: '备注',width:180}
                        ,{field: 'status', title: '状态',toolbar: '#statusText' ,width:80}
                        ,{field: 'start_time', title: '开始时间',sort: true,width:170}
                        ,{field: 'end_time', title: '结束时间',sort: true,width:170}
                        ,{field: 'created_at', title: '创建时间',sort: true,width:170}
                        ,{field: 'updated_at', title: '更新时间',sort: true,width:170}
                        ,{fixed: 'right',title: '操作', width: 160, align:'center', toolbar: '#options'}
                    ]]
                });

                //监听工具条
                table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        ,layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent === 'del'){
                        layer.confirm('确认删除吗？', function(index){
                            $.post("{{ route('admin.operate_position.destroy') }}",{_method:'delete',ids:[data.id]},function (result) {
                                if (result.code==0){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    layer.msg(result.msg,{icon:6})
                                }else{
                                    layer.msg(result.msg,{icon:5})
                                }
                                layer.close(index);
                            });
                        });
                    } else if(layEvent === 'edit'){
                        location.href = '/admin/operate_position/'+data.id+'/edit';
                    }
                });
                table.on('toolbar', function(obj){
                    if(obj.event=='delete'){ //按钮批量删除
                        var ids = []
                        var hasCheck = table.checkStatus('dataTable')
                        var hasCheckData = hasCheck.data
                        if (hasCheckData.length>0){
                            $.each(hasCheckData,function (index,element) {
                                ids.push(element.id)
                            })
                        }
                        if (ids.length>0){
                            layer.confirm('确认删除吗？', function(index){
                                $.post("{{ route('admin.operate_position.destroy') }}",{_method:'delete',ids:ids},function (result) {
                                    if (result.code==0){
                                        dataTable.reload();
                                        layer.msg(result.msg,{icon:6})
                                    }else {
                                        layer.msg(result.msg,{icon:5});
                                    }
                                    layer.close(index);
                                });
                            })
                        }else {
                            layer.msg('请选择删除项')
                        }
                    }else if(obj.event=='search'){
                        var title = $("#title").val();
                        var status = $("#status").val()
                        var start_time1 = $("#start_time1").val();
                        var start_time2 = $("#start_time2").val()
                        var end_time1 = $("#end_time1").val();
                        var end_time2 = $("#end_time2").val()
                        dataTable.reload({
                            where:{
                                title:title,
                                status:status,
                                start_time1:start_time1,
                                start_time2:start_time2,
                                end_time1:end_time1,
                                end_time2:end_time2
                            }
                        })
                    }
                });
            })
            layui.use('laydate', function(){
                var laydate = layui.laydate;
                //日期时间选择器
                laydate.render({
                    elem: '#start_time1'
                    ,type: 'datetime'
                });
                laydate.render({
                    elem: '#start_time2'
                    ,type: 'datetime'
                });
                laydate.render({
                    elem: '#end_time1'
                    ,type: 'datetime'
                });
                laydate.render({
                    elem: '#end_time2'
                    ,type: 'datetime'
                });
            });
            function transformTime(t){
                //注意：苹果手机不支持以“-”分割的时间形式，故必须进行格式转换。
                var time = t.replace(/-/g,"/").substring(0,t.length);
                return Date.parse(time);
            }
            function getNow() {
                var date = new Date();
                var seperator1 = "-";
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var strDate = date.getDate();
                var hours = date.getHours();
                var minutes = date.getMinutes();
                var seconds = date.getSeconds();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var currentdate = year + seperator1 + month + seperator1 + strDate+" "+hours+":"+minutes+":"+seconds;
                return currentdate;
            }
        </script>
    @endcan
@endsection